package com.fast.alden.admin.web.controller.system;

import com.fast.alden.admin.annotation.OperationLog;
import com.fast.alden.admin.entity.dto.AssignUserPositionParam;
import com.fast.alden.admin.service.SysPositionService;
import com.fast.alden.data.model.SysOperationType;
import com.fast.alden.data.model.SysPosition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位管理控制器
 */
@Tag(name = "SysPositionController", description = "岗位管理")
@RestController
@RequestMapping("/position")
public class SysPositionController {
    @Resource
    private SysPositionService positionService;

    @Operation(summary = "分页查询", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/page")
    public Page<SysPosition> page(@RequestBody @Nullable SysPosition searchVo,
                                  @RequestParam(name = "no", defaultValue = "0") int no,
                                  @RequestParam(name = "size", defaultValue = "10") int size
    ) {
        return positionService.search(searchVo, PageRequest.of(no, size, Sort.by(Sort.Direction.DESC, "createdTime")));
    }

    @Operation(summary = "根据ID查询", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @GetMapping("/retrieve/{id}")
    public SysPosition retrieve(@PathVariable("id") Long id) {
        return positionService.retrieve(id);
    }

    @OperationLog(title = "岗位管理", type = SysOperationType.CREATE)
    @Operation(summary = "创建岗位", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/create")
    public Long create(@RequestBody SysPosition position) {
        return positionService.create(position).getId();
    }

    @OperationLog(title = "岗位管理", type = SysOperationType.UPDATE)
    @Operation(summary = "修改岗位", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PutMapping("/update")
    public void update(@RequestBody SysPosition position) {
        positionService.update(position);
    }

    @OperationLog(title = "岗位管理", type = SysOperationType.DELETE)
    @Operation(summary = "删除岗位", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @DeleteMapping("/remove")
    public void remove(@RequestParam("ids") List<Long> ids) {
        positionService.remove(ids);
    }

    /**
     * 查找已分配的用户
     *
     * @param userId
     * @return
     */
    @Operation(summary = "查找指定岗位已分配的用户列表", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @GetMapping("/{userId}/positions")
    public List<SysPosition> findPositionsByUserId(@PathVariable("userId") Long userId) {
        return positionService.findPositionsByUserId(userId);
    }

    /**
     * 分配岗位
     */
    @OperationLog(title = "岗位管理", type = SysOperationType.UPDATE)
    @Operation(summary = "为指定用户列表分配指定岗位", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/assign")
    public void assign(@RequestBody @Validated AssignUserPositionParam param) {
        positionService.assign(param.getUserId(), param.getPositionIds());
    }

    /**
     * 取消分配岗位
     */
    @OperationLog(title = "岗位管理", type = SysOperationType.UPDATE)
    @Operation(summary = "为指定用户列表移除指定岗位", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/cancel-assign")
    public void cancelAssign(@RequestBody @Validated AssignUserPositionParam param) {
        positionService.cancelAssign(param.getUserId(), param.getPositionIds());
    }
}
